package com.eco.fanliapp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.dao.CommissionFreezeMapper;
import com.eco.fanliapp.entity.CommissionFreeze;
import com.eco.fanliapp.service.ICommissionFreezeService;
import com.eco.fanliapp.service.RedisTemplateService;
import com.eco.fanliapp.utils.DateFormatUtils;
import com.eco.fanliapp.vo.CommissionFreezeInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class CommissionFreezeServiceImpl extends ServiceImpl<CommissionFreezeMapper, CommissionFreeze> implements ICommissionFreezeService {
    @Autowired
    private CommissionFreezeMapper commissionFreezeMapper ;
    @Autowired
    private RedisTemplateService redisTemplateService;
    /**
     *  新写入数据库记录,web_commission_freeze
     *
     * @param record
     */
    @Override
    public ServerResponse insertOrUpdate(CommissionFreezeInfo record) {

        Integer tableId = record.getTableId();
       // CommissionFreeze commissionFreezeById = commissionFreezeMapper.selectByPrimaryKey(tableId);
        CommissionFreeze commissionFreeze = new CommissionFreeze();
        if (tableId == null){
            commissionFreeze.setCommissionlimit(record.getCommissionlimit());
            commissionFreeze.setDelaydays(record.getDelaydays());
            commissionFreeze.setDeviceWithdrawalAmount(record.getDeviceWithdrawalAmount());
            String deviceWithdrawalDate = record.getDeviceWithdrawalDate();
            Long longTime = DateFormatUtils.convertTimeToLong(deviceWithdrawalDate);
            commissionFreeze.setDeviceWithdrawalDate(longTime);
           // commissionFreeze.setDeviceWithdrawalDate(Long.parseLong(deviceWithdrawalDate));

            int insert = commissionFreezeMapper.insert(commissionFreeze);
            if (insert == 1){
                return ServerResponse.createBySuccessMessage("增加成功！");
            }
            return ServerResponse.createByErrorMessage("增加失败");
        }
        if (!StringUtils.isEmpty(record.getCommissionlimit())){
            commissionFreeze.setCommissionlimit(record.getCommissionlimit());
        }
        if (!StringUtils.isEmpty(record.getDelaydays())){
            commissionFreeze.setDelaydays(record.getDelaydays());
        }
        if (!StringUtils.isEmpty(record.getDeviceWithdrawalAmount())){
            commissionFreeze.setDeviceWithdrawalAmount(record.getDeviceWithdrawalAmount());
        }
        if (StringUtils.isEmpty(record.getDeviceWithdrawalDate())){
            String deviceWithdrawalDate = record.getDeviceWithdrawalDate();
            Long longTime = DateFormatUtils.convertTimeToLong(deviceWithdrawalDate);
            commissionFreeze.setDeviceWithdrawalDate(longTime);
        }
        int update = commissionFreezeMapper.updateByPrimaryKeySelective(commissionFreeze);
        //刷新redis
        this.selectByPrimaryKey(commissionFreeze.getTableId(), 0);
        if (update == 1){
            return ServerResponse.createBySuccessMessage("修改成功！");
        }
        return ServerResponse.createByErrorMessage("修改失败");
    }

    /**
     * 根据指定主键获取一条数据库记录,web_commission_freeze
     * @param tableId
     * @param type 1优先从redis 0优先从数据库
     * @return
     */
    @Override
    public CommissionFreeze selectByPrimaryKey(Integer tableId,int type) {
        if (tableId == null) {
            return null;
        }
        CommissionFreeze commissionFreeze = null;
        String redisKeyStr = "app_commissionFreeze_"+ tableId;
        if (type == 1 && redisTemplateService.hasKey(redisKeyStr)) {
            commissionFreeze = redisTemplateService.getTByKey(redisKeyStr, CommissionFreeze.class);
        }
        if (commissionFreeze == null) {
            commissionFreeze = commissionFreezeMapper.selectByPrimaryKey(tableId);
            if (commissionFreeze != null) {
                redisTemplateService.set(redisKeyStr, commissionFreeze, 60 * 5);
            }
        }
        return commissionFreeze;
    }
    /**
     *  根据指定主键获取一条数据库记录,web_commission_freeze
     *
     * @param tableId
     */
    @Override
    public ServerResponse<CommissionFreeze> selectByPrimaryKey(Integer tableId) {

        CommissionFreeze commissionFreeze = commissionFreezeMapper.selectByPrimaryKey(tableId);

        if (commissionFreeze == null){
            return ServerResponse.createByErrorMessage("查询失败");
        }
        return ServerResponse.createBySuccess("查询成功",commissionFreeze);
    }

    @Override
    public ServerResponse updateByPrimaryKey(CommissionFreeze record) {
        CommissionFreeze commissionFreeze = commissionFreezeMapper.selectByPrimaryKey(record.getTableId());
        if(StringUtils.isEmpty(commissionFreeze)){
            //int insert = commissionFreezeMapper.insert(record);
            boolean save = this.save(record);
            if (save){
                return ServerResponse.createBySuccessMessage("保存成功");
            }
            return ServerResponse.createBySuccessMessage("保存失败");
        }else {
            if(StringUtils.isEmpty(record.getCommissionlimit()) && StringUtils.isEmpty(record.getDelaydays()) && StringUtils.isEmpty(record.getDeviceWithdrawalAmount())){
                return ServerResponse.createBySuccessMessage("保存成功");
            }
            //int update = commissionFreezeMapper.updateByPrimaryKey(record);
            boolean b = this.updateById(record);
            //刷新redis
            this.selectByPrimaryKey(record.getTableId(), 0);
            if (b){
                return ServerResponse.createBySuccessMessage("保存成功");
            }
            return ServerResponse.createBySuccessMessage("保存失败");
        }

    }

	@Override
	public CommissionFreeze selectByPrimaryKeyLIMIT(int i) {
		// TODO Auto-generated method stub
		return commissionFreezeMapper.selectByPrimaryKeyLIMIT(i);
	}
}
